/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Controleur.Embarcadere;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Valentin
 */
public class EmbarcadereDAO extends DAO<Embarcadere> {

    public EmbarcadereDAO(Connection conn) {
        super(conn);
    }

    @Override
    public void insert(Embarcadere embarcadere) throws SQLException {
        String query = " INSERT INTO"
                + " embarcadere"
                + " (Ville, CodePostal, InfoTouristique)"
                + " VALUES (\"" + embarcadere.getVille() + "\"," + embarcadere.getCodePostal() + ",\"" + embarcadere.getInfoTouristique() + "\")";

        state.executeUpdate(query, state.RETURN_GENERATED_KEYS);
        try (ResultSet rs = state.getGeneratedKeys()) {
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            if (rs.next()) {
                do {
                    for (int i = 1; i <= columnCount; i++) {
                        int key = rs.getInt(i);
                        embarcadere.setId(key);
                    }
                } while (rs.next());
            } else {
                embarcadere.setId(-1);
            }
        }

    }

    @Override
    public void delete(Embarcadere embarcadere) throws SQLException {
        String query = " DELETE FROM"
                + " embarcadere"
                + " WHERE IDEmbarcadere ="
                + embarcadere.getId();

        state.executeUpdate(query);
    }

    @Override
    public void update(Embarcadere embarcadere) throws SQLException {
        String query = " UPDATE  embarcadere"
                + " SET Ville = " + "\"" + embarcadere.getVille() + "\""
                + " ,CodePostal = " + embarcadere.getCodePostal()
                + " ,InfoTouristique = " + "\"" + embarcadere.getInfoTouristique() + "\""
                + " WHERE IDEmbarcadere = " + embarcadere.getId();

        state.executeUpdate(query);
    }

    @Override
    public Embarcadere findByID(java.lang.Integer id) throws SQLException {

        Embarcadere myEmbarcadere = new Embarcadere();
        String query = " SELECT *"
                + " FROM embarcadere"
                + " WHERE IDEmbarcadere = "
                + id;

        queryResult = state.executeQuery(query);
        while (queryResult.next()) {
            myEmbarcadere.setId(queryResult.getInt("IDParticipant"));
            myEmbarcadere.setVille(queryResult.getString("Ville"));
            myEmbarcadere.setCodePostal(queryResult.getInt("CodePostal"));
            myEmbarcadere.setInfoTouristique(queryResult.getString("InfoTouristique"));
        }
        return myEmbarcadere;
    }

    public Embarcadere findEmbarcadereOfExcursion(String etape, int IDExcursion) throws SQLException {
        Embarcadere myEmbarcadere = new Embarcadere();
        String query = " SELECT *"
                + " FROM embarcadere em"
                + "  JOIN etape et on (et.IDEmbarcadere_fk = em.IDEmbarcadere)"
                + " WHERE et.IDExcursion_fk = " + IDExcursion
                + " AND et.TypeEtape = \"" + etape + "\"";



        queryResult = state.executeQuery(query);

        while (queryResult.next()) {
            myEmbarcadere.setId(queryResult.getInt("IDEmbarcadere"));
            myEmbarcadere.setVille(queryResult.getString("Ville"));
            myEmbarcadere.setCodePostal(queryResult.getInt("CodePostal"));
            myEmbarcadere.setInfoTouristique(queryResult.getString("InfoTouristique"));
            myEmbarcadere.setHeurePassage(queryResult.getTime("Heure"));

        }
        return myEmbarcadere;
    }

    @Override
    public Map<Integer, Embarcadere> findAll() throws SQLException {
        Map<Integer,Embarcadere> myMap = new HashMap<>();
        String query = " SELECT *"
                + " FROM embarcadere";

        queryResult = state.executeQuery(query);
        while (queryResult.next()) {
            Embarcadere myEmbarcadere = new Embarcadere();
            myEmbarcadere.setId(queryResult.getInt("IDEmbarcadere"));
            myEmbarcadere.setVille(queryResult.getString("Ville"));
            myEmbarcadere.setCodePostal(queryResult.getInt("CodePostal"));
            myEmbarcadere.setInfoTouristique(queryResult.getString("InfoTouristique"));
            myMap.put(myEmbarcadere.getId(), myEmbarcadere);
        }
        return myMap;
    }
    
    
    
}
